package com.gk.javaservice.util.base;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class Upload {
	// 文件下载
	public static void downloadFile(

	// 获取文件的物理路径
			String path, HttpServletResponse response) {
		try {
			File file = new File(path);

			String filename = path.substring(path.lastIndexOf("/") + 1);

			// 下载
			response.setCharacterEncoding("utf-8");
			response.setContentType("multipart/form-data");
			response.setHeader("content-disposition", "attachement;filename="
					+ filename);

			FileInputStream inputStream = new FileInputStream(file);
			OutputStream os = response.getOutputStream();
			byte[] b = new byte[1024];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				os.write(b, 0, length);
			}
			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	// 文件上传
	public static void uploadFile(MultipartFile file, String path) {
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(path);
			fos.write(file.getBytes());
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		String path = "65qw1e65/6qwe565/6666.zip";

		String projectName = "" + path.substring(path.lastIndexOf("/") + 1);
		// System.out.println(projectName);
	}
}
